package com.flute.icrawler.check.checker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.flute.icrawler.check.util.SerializeId;
import com.flute.icrawler.check.util.Util;

public class UpdateChecker {
	
	static UpdateChecker updateChecker  = new UpdateChecker();
	List<String> customUrlList = new ArrayList<String>();
	
	UpdateChecker(){
		customUrlList.add("http://192.168.1.125:8088/htmlTest/htmlTest.do?id=91534");
	}
	
	public static UpdateChecker getInstance(){
		return updateChecker;
	}
	
	public List<UpdateURI> readUpdateFile(File file){
		List<UpdateURI> updatetList = new ArrayList<UpdateURI>();
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
			String str_a; 
			while((str_a=br.readLine())!=null){
				if(str_a.indexOf("完成一次更新")!=-1){
					String url = Util.findTargetInStr("\\[(.+?)\\]", str_a);
					String updateCount = Util.findTargetInStr("当前更新总数=(.+?)$", str_a);
					String updateDate = Util.findTargetInStr("([0-9]{4}\\-[0-9]{2}\\-[0-9]{2}\\s[0-9]{2}\\:[0-9]{2}\\:[0-9]{2})", str_a);
					UpdateURI updteURI = new UpdateURI();
					if(url.indexOf("?") == -1){
						url = url  + "index.jsp";
					}
					Pattern pattern = Pattern.compile("[0-9]{4}/(.*)");
				    Matcher matcher = pattern.matcher(url);
				    if (matcher.find()) {
				    	url = matcher.group(1);
				    }
					updteURI.setUrl(url);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					updteURI.setUpdateTime(sdf.parse(updateDate));
					updatetList.add(updteURI);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  updatetList;
	}
	public List<UpdateURI> readTestFile(File file){
		List<UpdateURI> testList = new ArrayList<UpdateURI>();
		BufferedReader br;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
			String str_a; 
			while((str_a=br.readLine())!=null){
					String updateTime = Util.findTargetInStr("([0-9]{4}\\-[0-9]{2}\\-[0-9]{2}\\s[0-9]{2}\\:[0-9]{2}\\:[0-9]{2})", str_a);
					if(updateTime.equals("")){
						System.out.println(file.getName());
						System.out.println(str_a);
						
					}
					String url = Util.findTargetInStr("(http.*)", str_a);
					UpdateURI updteURI = new UpdateURI();
					
					Pattern pattern = Pattern.compile("[0-9]{4}/(.*)");
				    Matcher matcher = pattern.matcher(url);
				    if (matcher.find()) {
				    	url = matcher.group(1);
				    }
					updteURI.setUrl(url);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					updteURI.setUpdateTime(sdf.parse(updateTime));
					testList.add(updteURI);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  testList;
	}
	public List<UpdateURI> merge(List<UpdateURI> a_List,List<UpdateURI> b_List){
		for(UpdateURI updteURI : b_List){
			a_List.add(updteURI);
		}
		return a_List;
	}
	
	public static void main(String args[]){
		String fileName = "/result.txt";
		String dir =   Util.getRootDir("testUpdateFiles") ;
		File file = new File(dir);
		UpdateChecker checker = UpdateChecker.getInstance();
		List<UpdateURI> testList = new ArrayList<UpdateURI>();
		for(File files : file.listFiles()){
			List<UpdateURI> list  =  checker.readTestFile(files);
			checker.merge(testList,list);
		}
		
		String dir_Update =   Util.getRootDir("CrawlerUpdateFiles");
		File file_U = new File(dir_Update);
		List<UpdateURI> updateList = new ArrayList<UpdateURI>();
		for(File files : file_U.listFiles()){
			List<UpdateURI> list  =  checker.readUpdateFile(files);
			checker.merge(updateList,list);
		}
		
		for(UpdateURI u : testList){
			//System.out.println(" TEST URL：" + u.getUrl());
			if(updateList.contains(u)){
				System.out.println(" 该URL被双方更新：" + u.getUrl());
			}else{
				System.out.println(" 该URL没有被更新模块跟新：" + u.getUrl() + "时间：" + u.getUpdateTime().toLocaleString());
			}
		}
//		for(UpdateURI u : updateList){
//			//System.out.println(" UPdate URL：" + u.getUrl());
//			if(!updateList.contains(u)){
//				//System.out.println(" 该URL没有被更新模块更新：" + u.getUrl());
//			}
////			if(updateList.contains(u)){
////				System.out.println(" 该URL被双方更新：" + u.getUrl());
////			}
//		}
		Map<String, Long> map  = Util.getRateMap();
    			Iterator<String>  it = map.keySet().iterator();
    			while(it.hasNext()){
    				String str =  it.next();
    				//System.out.println(" id =" + str  + " 频率 = "+map.get(str));
    			}
    	
		
	}

}
